package org.dromara.system.service.impl;

import org.dromara.common.core.constant.Constants;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.vo.XcxFollowVo;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.XcxFootprintBo;
import org.dromara.system.domain.vo.XcxFootprintVo;
import org.dromara.system.domain.XcxFootprint;
import org.dromara.system.mapper.XcxFootprintMapper;
import org.dromara.system.service.IXcxFootprintService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 我的足迹Service业务层处理
 *
 * @author Lion Li
 * @date 2024-05-08
 */
@RequiredArgsConstructor
@Service
public class XcxFootprintServiceImpl implements IXcxFootprintService {

    private final XcxFootprintMapper baseMapper;

    /**
     * 查询我的足迹
     */
    @Override
    public XcxFootprintVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询我的足迹列表
     */
    @Override
    public TableDataInfo<XcxFootprintVo> queryPageList(XcxFootprintBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<XcxFootprint> lqw = buildQueryWrapper(bo);
        Page<XcxFootprintVo> result = baseMapper.selectVoPageZdy(bo.getItemName(),pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询我的足迹列表
     */
    @Override
    public List<XcxFootprintVo> queryList(XcxFootprintBo bo) {
        LambdaQueryWrapper<XcxFootprint> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<XcxFootprint> buildQueryWrapper(XcxFootprintBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<XcxFootprint> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getFootprintId() != null, XcxFootprint::getFootprintId, bo.getFootprintId());
        lqw.eq(bo.getCreateBy() != null, XcxFootprint::getCreateBy, bo.getCreateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getFootprintType()), XcxFootprint::getFootprintType, bo.getFootprintType());
        lqw.eq(XcxFootprint::getFootStatus,Constants.STATUS_NORMAL);
        return lqw;
    }

    /**
     * 新增我的足迹
     */
    @Override
    public Boolean insertByBo(XcxFootprintBo bo) {
        bo.setCreateBy(LoginHelper.getUserId());
        bo.setUpdateBy(LoginHelper.getUserId());
        XcxFootprint add = MapstructUtils.convert(bo, XcxFootprint.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改我的足迹
     */
    @Override
    public Boolean updateByBo(XcxFootprintBo bo) {
        bo.setUpdateBy(LoginHelper.getUserId());
        XcxFootprint update = MapstructUtils.convert(bo, XcxFootprint.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(XcxFootprint entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除我的足迹
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (Long id:ids) {
            XcxFootprintVo xcxFootprintVo = baseMapper.selectVoById(id);
            if(xcxFootprintVo !=null){
                xcxFootprintVo.setFootStatus(Constants.STATUS_DEL);
                XcxFootprint update = MapstructUtils.convert(xcxFootprintVo, XcxFootprint.class);
                //逻辑删除
                baseMapper.updateById(update);
            }

        }
        return true;
    }

    @Override
    public TableDataInfo<XcxFootprintVo> queryPageListXcx(XcxFootprintBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<XcxFootprint> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(XcxFootprint::getUpdateTime);
        Page<XcxFootprintVo> result = baseMapper.selectVoPageZdy(bo.getItemName(),pageQuery.build(), lqw);
        for (XcxFootprintVo xcxFootprintVo : result.getRecords()){
            if("01".equals(xcxFootprintVo.getFootprintType())){
                xcxFootprintVo.setTitle(xcxFootprintVo.getWbItemInfoVo().getItemName());
                xcxFootprintVo.setPushDate(xcxFootprintVo.getWbItemInfoVo().getPublishTime());
            }else if("03".equals(xcxFootprintVo.getFootprintType())){
                xcxFootprintVo.setTitle(xcxFootprintVo.getWbMinuteBidBagVo().getItemName());
                xcxFootprintVo.setPushDate(xcxFootprintVo.getWbMinuteBidBagVo().getBidDate());
            }else if("04".equals(xcxFootprintVo.getFootprintType())){
                xcxFootprintVo.setTitle(xcxFootprintVo.getWbCandidateInfoVo().getHeadline());
                if(xcxFootprintVo.getWbCandidateInfoVo().getPublicityTime() != null){
                    xcxFootprintVo.setPushDate(DateUtils.dateTime(xcxFootprintVo.getWbCandidateInfoVo().getPublicityTime()));
                }

            }
        }
        return TableDataInfo.build(result);
    }

    @Override
    public XcxFootprintVo queryListZYT(XcxFootprintBo xcxFootprintBo) {
        LambdaQueryWrapper<XcxFootprint> lqw = buildQueryWrapper(xcxFootprintBo);
        XcxFootprintVo xcxFootprintVo =  baseMapper.selectVoPageZYT(lqw);
        return xcxFootprintVo;
    }
}
